package com.yoso.shop.interfaces.controller.user;

import com.alibaba.fastjson.JSON;
import com.yoso.shop.application.service.IAdminService;
import com.yoso.shop.domain.user.admin.AdminEntity;
import com.yoso.shop.domain.user.admin.AdminEntityExample;
import com.yoso.shop.infrastructure.base.controller.BaseController;
import com.yoso.shop.infrastructure.base.controller.BaseResponse;
import com.yoso.shop.infrastructure.base.service.IBaseService;
import com.yoso.shop.interfaces.vo.user.admin.AdminVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author shizejin
 */
@RestController
@RequestMapping("/admin")
@Api("管理员")
@Slf4j
public class AdminInfoController extends BaseController<AdminVo,AdminEntity> {

    @Resource
    private IAdminService adminService;
    @Override
    public AdminEntity V2E(AdminVo data) {

        return JSON.parseObject(JSON.toJSONString(data), AdminEntity.class);
    }

    @Override
    public AdminVo E2V(AdminEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), AdminVo.class);
    }

    @Override
    public IBaseService<AdminEntity, AdminEntityExample> getService() {
        return this.adminService;
    }

    /**
     * 管理员的登录
     */
    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public BaseResponse<Void> login(@RequestBody AdminVo admin){
        AdminVo result = adminService.login(V2E(admin));
        return !ObjectUtils.isEmpty(result) ? this.success("验证成功") : this.error("验证失败");
    }

//    @PostMapping("/add")
    @Override
    public BaseResponse<Void> saveOrUpdate(@RequestBody AdminVo admin){
        String username = admin.getUsername();
        String password = admin.getPassword();
        if (StringUtils.isAnyBlank(username,password)){{
            return this.error("用户名和密码不能为空");
        }}
        int result = this.getService().insert(V2E(admin));
        return this.result(result);
    }
}
